"""Statistics API schemas."""

from __future__ import annotations

from datetime import datetime

from pydantic import Field

from app.schemas.base import ORMBase


class CurrentValue(ORMBase):
    """Current (latest) metric value."""

    device_id: int
    metric_name: str
    value: float
    timestamp: datetime
    quality: str
    unit: str | None = None


class AggregatedStats(ORMBase):
    """Aggregated statistics for a metric over time window."""

    device_id: int
    metric_name: str
    time_window_hours: int
    count: int = Field(..., description="Number of data points in window")
    min_value: float | None = Field(None, description="Minimum value")
    max_value: float | None = Field(None, description="Maximum value")
    avg_value: float | None = Field(None, description="Average value")
    start_time: datetime | None = Field(None, description="First timestamp in window")
    end_time: datetime | None = Field(None, description="Last timestamp in window")


class DeviceSummary(ORMBase):
    """Summary statistics for a device."""

    device_id: int
    metric_count: int = Field(..., description="Number of distinct metrics")
    total_readings: int = Field(..., description="Total number of readings")
    first_reading: datetime = Field(..., description="Timestamp of first reading")
    last_reading: datetime = Field(..., description="Timestamp of last reading")
    time_window_hours: int


class HistogramBucket(ORMBase):
    """Single bucket in value distribution histogram."""

    range_start: float
    range_end: float
    count: int


class MetricDistribution(ORMBase):
    """Value distribution (histogram) for a metric."""

    device_id: int
    metric_name: str
    time_window_hours: int
    min_value: float | None = None
    max_value: float | None = None
    buckets: list[HistogramBucket] = Field(..., description="Histogram buckets")
